package mygameclient;

import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.AbstractTableModel;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;


public class PlayerTableModel extends AbstractTableModel {

    private ArrayList<Player> players = new ArrayList<Player>();
    private Set<TableModelListener> listeners = new HashSet<TableModelListener>();
    private TableModelEvent event;

    public void addTableModelListener(TableModelListener listener) {
        listeners.add(listener);
    }

    public PlayerTableModel(ArrayList<Player> players) {
        super();
        this.players = players;
        event = new TableModelEvent(this);
        addTableModelListener(new TableModelListener() {
            @Override
            public void tableChanged(TableModelEvent e) {
                fireTableChanged(event);
            }
        });
    }

    public Object getValueAt(int rowIndex, int columnIndex) {
        Player player = players.get(rowIndex);
        switch (columnIndex) {
            case 0:
                return player.getpHashCode();
            case 1:
                return player.getpName();
            case 2:
                return player.getpAdress();
            case 3:
                return player.getpPort();
        }
        fireTableChanged(event);
        return "";
    }

    public int getRowCount() {
        return players.size();
    }

    public int getColumnCount() {
        return 4;
    }

    public String getColumnName(int columnIndex) {
        switch (columnIndex) {
            case 0:
                return "player_id";
            case 1:
                return "Name";
            case 2:
                return "Adress";
            case 3:
                return "Port";
        }
        return "";
    }

    public boolean isCellEditable(int row, int column) {
        return false;
    }
}
